package espaceVisuCarre.java2Dutil;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.font.FontRenderContext;
import java.awt.font.LineMetrics;
import java.awt.font.TextLayout;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Collection;
import java.util.LinkedList;

import xmlItunes.Track;

public class TextDrawable implements IDrawable
{
    private Color color ;
    private String texte ;
    private Font f ; 
    
    private Rectangle2D boundingRect ;
    
    private LinkedList<Track> tracks ;
    
    
    public TextDrawable(Color color, Rectangle2D.Float rect , String s)
    {
            this.texte = s ;
            this.color = color ;
            f = new Font("Helvetica",Font.BOLD, Math.max( (int)rect.height, (int)rect.width ) );
            
            boundingRect = rect ;
    }

    @Override
    public void draw(Graphics2D g) 
    {
            FontRenderContext frc = g.getFontRenderContext();               
            
            TextLayout tl = new TextLayout(texte, f, frc);
            Rectangle2D bounds = f.getStringBounds(texte, frc);
            LineMetrics metrics = f.getLineMetrics(texte, frc);
            
            float width = (float) bounds.getWidth();     // The width of our text
            float height = (float) bounds.getHeight();     // The height of our text

            while( width > boundingRect.getWidth() || height > boundingRect.getHeight() )
            {
                int oldSize = f.getSize() ;
                f = new Font(f.getFontName() , f.getStyle() , oldSize-1);
            	
            	tl = new TextLayout(texte, f, frc);
                bounds = f.getStringBounds(texte, frc);
                metrics = f.getLineMetrics(texte, frc);
                
                width = (float) bounds.getWidth();     // The width of our text
                height = (float) bounds.getHeight();     // The height of our text

            }
            
            Color c = g.getColor() ;
            
            g.setColor(this.color);
            tl.draw(
            			g,
            			(int)( boundingRect.getX() + ( boundingRect.getWidth() - tl.getBounds().getWidth() )/2   ) ,
            		    (int)( boundingRect.getCenterY() + tl.getBounds().getHeight()/2  ) 
            	    );
            //g.draw(boundingRect); 
            
            //g.setColor(Color.blue);
            //g.draw( new Rectangle2D.Float(  (float)( boundingRect.getX() + ( boundingRect.getWidth() - tl.getBounds().getWidth() )/2   ) , (float)( boundingRect.getY() + boundingRect.getHeight()/2  -   (tl.getBounds().getHeight()/2) )  , (float)tl.getBounds().getWidth() ,(float) tl.getBounds().getHeight() ) );
            
            g.setColor(c);
    }

    @Override
    public boolean contains(Point2D p) 
    {
            return boundingRect.contains(p);
    }

    @Override
    public Rectangle2D.Float getRectangle() 
    {
            return (Rectangle2D.Float)boundingRect.clone();
    }
    
    
	public Collection<Track> getTracks() 
	{
		if(tracks == null)return new LinkedList<Track>();
		return tracks;
	}
	
	public void setTracks( Collection<Track> tr ) 
	{
		this.tracks = new LinkedList<Track>(tr);
	}

	public String getTexte() {
		return texte;
	}

}
